/**
 * 订单管理管理初始化
 */
var TOrder = {
    id: "TOrderTableSum",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TOrder.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left',fixed: 'left'},
        {title: '编号', field: 'originalNum',width: 120, visible: true, align: 'center', valign: 'middle',fixed: 'left',

            templet: function (d) {

                if(d.isTransfer==1){
                    if (d.flow_status <2&&d.days>=3&&d.days<10) {
                        return '<div style="background-color: #dddd00">' + d.originalNum + '</div>'
                    }else if(d.flow_status <2&&d.days>=10){
                        return '<div style="background-color: #FF6347">' + d.originalNum + '</div>'
                    }else{
                        return  d.originalNum;
                    }
                }else{
                    if(d.MODE==0){
                        if (d.days1>=3&&d.days1<10) {
                            return '<div style="background-color: #dddd00">' + d.originalNum + '</div>'
                        }else if(d.days1>=10){
                            return '<div style="background-color: #FF6347">' + d.originalNum + '</div>'
                        }else{
                            return  d.originalNum;
                        }
                    }else{
                        if (d.flow_status <2&&d.days>=3&&d.days<10) {
                            return '<div style="background-color: #dddd00">' + d.originalNum + '</div>'
                        }else if(d.flow_status <2&&d.days>=10){
                            return '<div style="background-color: #FF6347">' + d.originalNum + '</div>'
                        }else{
                            return  d.originalNum;
                        }
                    }
                }

            }

        },
        {title: '上游物流', field: 'sendDeptName',width: 100, visible: true, align: 'center', valign: 'middle',fixed: 'left'},
        {title: '收货地址', field: 'recAddress',width: 150, visible: true, align: 'center', valign: 'middle',fixed: 'left'},
        {title: '收货人', field: 'consignee',width: 100, visible: true, align: 'center', valign: 'middle',fixed: 'left'},
        {title: '物品名称', field: 'goodsName',width: 100, visible: true, align: 'center', valign: 'middle',fixed: 'left'},
        {title: '配送方式', field: '',width: 80, visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
            if(d.isTransfer==1){
                return "中转";
            }else{
                if(d.MODE==0){
                    return "自提";
                }else{
                    return "配送";
                }
            }
            return ""

        }},
        {title: '件数', field: 'goodsPiece',width:80, visible: true, align: 'center', valign: 'middle'},
        {title: '回单要求', field: 'receipt',width: 80, visible: true, align: 'center', valign: 'middle'},
        {title: '订单状态', field: 'statusName',width: 80, visible: true, align: 'center', valign: 'middle',  templet: function (d) {
            if(d.flow_status==0){
                return "开单";
            }else if(d.flow_status==1){
                return "库存";
            }else if(d.flow_status==2){
                return "出库";
            }else if(d.flow_status==3){
                return "收银";
            }else if(d.flow_status==4){
                return "核销";
            }
            return ""

        }
        },
        {title: '上游款', field: 'sy', visible: true, width: 80,align: 'center', valign: 'middle'},
        {title: '下游款款', field: 'xy', visible: true,width: 80, align: 'center', valign: 'middle'},
        {title: '利润', field: 'lr', visible: true,width: 80, align: 'center', valign: 'middle',
            templet: function (d) {
                return (d.sy.add(d.xy));
        }},
        {title: '司机运费', field: 'sjyf', visible: true,width: 80, align: 'center', valign: 'middle'},
        {title: '入库时间', field: 'createtime', visible: true, align: 'center',width: 220, valign: 'middle'},
        {title: '回单时间', field: 'send_time', visible: true, align: 'center',width: 220, valign: 'middle'},
        {title: '接收时间', field: 'receive_time', visible: true, align: 'center',width: 220, valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
TOrder.check = function () {

    var selected = mTable.checkStatus(TOrder.id);
    if (selected.data.length == 0 || selected.data.length > 1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TOrder.seItem = selected.data[0];
        return true;
    }
};

TOrder.collectSum = function() {
    // if(m){
    //     mDate=m;
    //     this.vDate=m.data;
    // }
    // var syhj=0;
    // var xyhj=0;
    // for(var i=0;i<mDate.data.length;i++){
    //     console.log(syhj +" "+mDate.data[i].xy+"  "+mDate.data[i].lr);
    //     syhj= syhj.add(mDate.data[i].sy);
    //     xyhj= xyhj.add(mDate.data[i].xy);
    // }

    var ajax = new $ax(Feng.ctxPath + "/tOrder/summaryListAvg", function (data) {
        $('#syhj').html(data[0].syhj);
        $('#xyhj').html(data[0].xyhj);
        $('#lrhj').html(data[0].lrhj);
    }, function (data) {
        // Feng.error("通知失败!" + data.responseJSON.message + "!");
    });
    ajax.set("name");
    ajax.set("beginTime");
    ajax.set("endTime");
    ajax.set("region");
    ajax.set("sy_money");
    ajax.set("xy_money");
    ajax.set("flag","1");
    ajax.set("dj_type",$("#dj_type").val()+"");
    ajax.set("orderStatus",$("#status").val()+"");
    ajax.set("auditStatus",$("#audit_status").val()+"");
    ajax.set("status",$("#status").val()+"");
    ajax.start();




}

/**
 * 打开查看订单管理详情
 */
TOrder.detailOrder = function () {
    if (this.check()) {
        window.open(Feng.ctxPath + '/tOrder/summaryDetail/' + TOrder.seItem.id)
    }
};
/**
 * 查询收银中心列表
 */
TOrder.search = function () {
    var dj_type=$("#dj_type").val()+"";
    var auditStatus=$("#audit_status").val()+"";
    var orderStatus=$("#status").val()+"";
    var status=$("#status").val()+"";
    if(orderStatus=="0"){
        orderStatus="";
    }
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['dj_type'] = dj_type;
    queryData['orderStatus'] = orderStatus;
    queryData['auditStatus'] = auditStatus;
    queryData['status'] = status;
    queryData['region'] = $("#region").val();
    queryData['sy_money'] = $("#sy_money").val();
    queryData['xy_money'] = $("#xy_money").val();
    queryData['flag'] ="1";

    aTable.refresh(queryData);
    TOrder.collectSum();
};

/**
 * 删除订单管理
 */
TOrder.updateStatus = function () {

        var ajax = new $ax(Feng.ctxPath + "/tOrder/updateStatus", function (data) {
            Feng.success("操作成功!");
            aTable.refresh();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("tOrderId", $('#id').val());
        ajax.start();

};
TOrder.exportOrder

TOrder.exportOrder = function () {
    var dj_type=$("#dj_type").val()+"";
    var auditStatus=$("#audit_status").val()+"";
    var orderStatus=$("#status").val()+"";
    var status=$("#status").val()+"";
    if(orderStatus=="0"){
        orderStatus="";
    }
    location.href=Feng.ctxPath + "/tOrder/exportSummaryList?dj_type="+dj_type+'&auditStatus='+auditStatus+'&orderStatus='+orderStatus
    +'&status='+status+'&name='+$("#name").val()+'&beginTime='+$("#beginTime").val()+'&endTime='+$("#endTime").val()
    +'&region='+$("#region").val()+'&sy_money='+$("#sy_money").val()+'&xy_money='+$("#xy_money").val();

};

var aTable = null
var mTable = null;
$(function () {

    // var position = $('#offHight').offset();
    // var top = position.top;
    var defaultColunms = TOrder.initColumn();
    aTable = new LYTable(TOrder.id,  "/tOrder/summaryList", defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        // aTable.setHeight($(".gray-bg").height()-top-50)
        TOrder.table = aTable.init(mTable);
    })

    $('#dj_type').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:""
    });
    $('#audit_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:""
    });
    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:""
    });
    $('#sy_money').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:""
    });
    $('#xy_money').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:""
    });
});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        TOrder.search();
    }
});


